package com.lx.demo.b.service.demob;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.codingapi.tx.annotation.TxTransaction;
import com.lx.Remessages;
import com.lx.Request;
import com.lx.Response;
import com.lx.demo.b.dal.entity.Student;
import com.lx.demo.b.dal.mapper.StudentMapper;
import com.lx.demo.b.interfaces.TestDemoBDubboService;
import com.lx.demo.b.interfaces.dto.PersonTestInfoDTO;
import com.lx.demo.b.interfaces.dto.TestDemoBDubboServiceDTO;
import com.lx.demo.interfaces.TestDubboDemoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by lanyxp on 2021/2/25 10:11
 */

@Slf4j
@com.alibaba.dubbo.config.annotation.Service
@RequiredArgsConstructor
public class TestDemoBDubboServiceImpl implements TestDemoBDubboService {

    private final StudentMapper studentMapper;

    @Transactional(rollbackFor = Exception.class)
    @TxTransaction
    @Override
    public Response addDataTestB(Request<TestDemoBDubboServiceDTO> req) {
        log.info("开始请求DUBOO服务>>>>请求参数:{}", JSON.toJSONString(req));
        String name = req.getBody().getName();
        Student student = new Student();
        student.setAge(1);
        student.setName(name);
        final int ids = this.studentMapper.insert(student);
        log.info("请求DUBOO服务结束>>>>返回参数:{}", JSON.toJSONString(Remessages.success(ids)));
        return Remessages.success(ids);
    }
    @TxTransaction
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Response insertIntoDB(Request<PersonTestInfoDTO> request) {
        log.info("开始请求DUBOO服务>>>>请求参数:{}", JSON.toJSONString(request));
        Integer age = request.getBody().getAge();
        String name = request.getBody().getName();
        Student student = new Student();
        student.setAge(age);
        student.setName(name);
        final int ids = this.studentMapper.insert(student);
        log.info("请求DUBOO服务结束>>>>返回参数:{}", JSON.toJSONString(Remessages.success(ids)));
        return Remessages.success(ids);
    }
}
